package com.abe.libquick.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.TextView;

import androidx.annotation.IntDef;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class DrawableHelper {
    public static final int TOP = 0x001;
    public static final int BOTTOM = 0x002;
    public static final int LEFT = 0x003;
    public static final int RIGHT = 0x004;


    public static void setLeftDrawable(Context context, TextView textView, int drawable, int wh) {
        setDrawable(context, textView, drawable, wh, LEFT);
    }

    public static void setRightDrawable(Context context, TextView textView, int drawable, int wh) {
        setDrawable(context, textView, drawable, wh, RIGHT);
    }

    /**
     * @param wh 单位dp 宽高相同
     */
    public static void setDrawable(Context context, TextView textView, int drawable, int wh, @DrawablePOS int pos) {
        setDrawable(context, textView, drawable, wh, wh, pos);
    }

    /**
     * @param w 单位dp
     * @param h 单位dp
     */
    public static void setDrawable(Context context, TextView textView, int drawable, int w, int h, @DrawablePOS int pos) {
        Drawable img = context.getResources().getDrawable(drawable);
        int wPx = NormalUtils.dip2px(context, w);
        int hPx = NormalUtils.dip2px(context, h);
        img.setBounds(0, 0, wPx, hPx);
        switch (pos) {
            case TOP:
                textView.setCompoundDrawables(null, img, null, null);
                break;
            case BOTTOM:
                textView.setCompoundDrawables(null, null, null, img);
                break;
            case LEFT:
                textView.setCompoundDrawables(img, null, null, null);
                break;
            case RIGHT:
                textView.setCompoundDrawables(null, null, img, null);
                break;
        }
    }

    @IntDef({TOP, BOTTOM, LEFT, RIGHT})
    @Retention(RetentionPolicy.SOURCE)
    public @interface DrawablePOS {
    }
}